<!--
 * @Description: 招聘企业列表
 * @Author: liyujie
 * @Date: 2021/09/22
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/09/22
-->
<template>
    <PageLayout title="招聘企业列表">
        <template slot="header-right">
            <el-button
                type="primary"
                size="medium"
                icon="el-icon-setting"
                :disabled="!authResult.auth_config"
                @click="handleRoute('recruitConfig')"
            >
                全局设置
            </el-button>
            <el-button
                type="primary"
                size="medium"
                :disabled="!authResult.auth_add"
                icon="el-icon-plus"
                @click="handleAddShop"
            >
                添加招聘商家
            </el-button>
        </template>
        <!-- 筛选-->
        <template slot="filter">
            <div class="filter-item">
                <div class="filter-item__title">
                    企业名称
                </div>
                <el-input
                    placeholder="企业名称"
                    v-model="filterData.keyword"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    @change="getTableData(1)"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    状态
                </div>
                <el-select
                    v-model="filterData.status"
                    filterable
                    size="medium"
                    @change="getTableData(1)"
                >
                    <el-option
                        label="全部"
                        :value="99"
                    />
                    <el-option
                        label="招聘中"
                        :value="1"
                    />
                    <el-option
                        label="已关闭"
                        :value="0"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <el-button
                    type="primary"
                    size="small"
                    class="query-button"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>
            </div>
        </template>
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="tempLateName"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
            />
            <el-table-column
                prop="company_name"
                label="公司名称"
                show-overflow-tooltip
                align="left"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="shop_group_title"
                label="商家类型"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="地区"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.province }}<span v-if="scope.row.city">/{{ scope.row.city }}</span><span v-if="scope.row.area">/{{ scope.row.area }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="releases_count"
                label="发布岗位数"
                align="center"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="apply_count"
                label="简历投递数"
                align="center"
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="公司行业"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.category_parent_title }}-{{ scope.row.category_title }}
                </template>
            </el-table-column>
            <el-table-column
                label="最近一次发布时间"
                align="left"
                :formatter="emptyFormatter"
                width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.last_releases_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column
                label="状态"
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            class="status-dot"
                            :class="['status-dot', scope.row.status === 1 ? 'success' : 'danger']"
                        />
                        <div>{{ scope.row.status | statusFilter }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
                width="180"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="beforeSetStatus(scope.row)"
                        >
                            {{ scope.row.status ? '关闭招聘' : '开放招聘' }}
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute('recruitCompanyDetail', {hire_no: scope.row.hire_no, company_name: scope.row.company_name}, 'auth_detail')"
                        >
                            查看详情
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
import {shopSelectDialog} from "@/components/shop-manage/shop-select";

export default {
    name: 'RecruitCompanyList',
    mixins: [back],
    components: {},
    data() {
        return {
            filterData: {
                keyword: '',
                status: 99,
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            authResult: {}
        };
    },
    computed: {},
    watch: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '已关闭';
                case 1:
                    return '招聘中';
                default:
                    return '--';
            }
        },
    },
    mounted() {
        this.init();
    },

    methods: {
        init() {
            let filterData = JSON.parse(sessionStorage.getItem('RECRUIT_COMPANY_QUERY'));

            this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '人力资源'}, {title: '招聘企业列表'}]);
            this.authResult = this.$route.meta.authResult;
            if (filterData) {
                this.filterData = filterData;
                this.page.now_page = Number(sessionStorage.getItem('RECRUIT_COMPANY_PAGE_NO'));
                sessionStorage.removeItem('RECRUIT_COMPANY_QUERY');
                sessionStorage.removeItem('RECRUIT_COMPANY_PAGE_NO');
            }
            this.getTableData();
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/work_recruit%5Cget_company_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: res.msg
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         * @param auth_name
         */
        handleRoute(routeName, query, auth_name) {
            if(auth_name && !this.authResult[auth_name]) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$router.push({
                name: routeName,
                query
            });

            sessionStorage.setItem('RECRUIT_COMPANY_QUERY', JSON.stringify(this.filterData));
            sessionStorage.setItem('RECRUIT_COMPANY_PAGE_NO', this.page.now_page);
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 选择商家
         * @returns {Promise<void>}
         */
        async handleAddShop() {
            let result = await shopSelectDialog({
                shopType: 1
            });

            if(result) {
                let hire_nos = result.map(item => {
                    return item.hire_no;
                }).join();

                this.$post('/student/work_recruit%5Cadd_company', {
                    hire_nos
                }, res => {
                    if (res.code === 1) {
                        this.$notify({
                            title: '提示',
                            type: 'success',
                            message: '添加成功'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            type: 'warning',
                            message: res.msg
                        });
                    }
                });
            }
        },

        /**
         * 设置状态前判断
         * @param row
         */
        beforeSetStatus(row) {
            if(!this.authResult.auth_set_status) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            if(row.status) {
                this.$zdConfirm('关闭后该企业所有的招聘信息用户端将会被下架，确认关闭吗？', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.handleSetStatus(row.hire_no, 0);
                }).catch(() => {});
            } else {
                this.handleSetStatus(row.hire_no, 1);
            }
        },

        /**
         * 设置状态请求
         * @param hire_no
         * @param status
         */
        handleSetStatus(hire_no, status) {
            this.$post('/student/work_recruit%5Cset_company_work_recruit_status', {
                hire_no,
                status
            }, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        type: 'success',
                        message: '操作成功'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: res.msg
                    });
                }
            });
        }
    }
};
</script>
